import  pymysql





class dataout(object):

    def __init__(self):

        self.db = pymysql.connect("localhost", "root", "root", "python", charset='utf8mb4', )

        self.cursor = self.db.cursor()

        self.cursor.execute("DROP TABLE IF EXISTS jinridata")

        self.cursor.execute("DROP TABLE IF EXISTS jinrigroupdata")

        self.cursor.execute("""CREATE TABLE jinridata (
                                                      标题 CHAR(200) NOT NULL,
                                                     关键字  TEXT ,
                                                     文章链接 CHAR(200),  
                                                     评论链接 CHAR(200),
                                                     作者  CHAR(50),
                                                     文章ID CHAR(50) 

                                      )""")


        self.cursor.execute("""CREATE TABLE jinrigroupdata (
                                                           标题 CHAR(200) NOT NULL,
                                                             内容  TEXT,
                                                             用户 CHAR(200),
                                                             用户id CHAR(200))""")


        self.db.close()

        print('配置成功')


    def output(self,data,data_group):

        '''
        
        :param data: 
        :param data_group: 
        :return: 
        '''
        db = pymysql.connect("localhost", "root", "root", "python", charset='utf8mb4', )

        cursor = db.cursor()

        print(data,data_group)

        for data_a in data:

            if self._get_SELECT(data_a['group_id'],cursor):

                sql_data = "INSERT INTO jinridata(标题, \
                               关键字, 文章链接, 评论链接,作者,文章ID) \
                               VALUES ('%s', '%s', '%s', '%s', '%s','%s')" % \
                           (data_a['title'],"".join(data_a['label']), data_a['url'], data_a['group_id_url'],data_a['source'],data_a['group_id']
                            )

                try:
                    cursor.execute(sql_data)
                    # 提交到数据库执行
                    db.commit()



                except Exception as e:
                    # 如果发生错误则回滚
                    print('发生错误',e)
                    db.rollback()
        print('热点写入成功')
        for group in data_group:

            sql_group = "INSERT INTO jinrigroupdata(标题, \
                                                           内容, 用户, 用户id) \
                                                           VALUES ('%s', '%s', '%s', '%s')" % \
                        (group['title'],pymysql.escape_string(group['text']),group['userid'],group['username']
                        )
            try:
                # 执行sql语句
                cursor.execute(sql_group)


                # 提交到数据库执行
                db.commit()

            except Exception as e:
                # 如果发生错误则回滚
                print('发生错误', e,group['text'],group['userid'],group['username'])

                db.rollback()
        print('评论写入成功')
        db.close()





    def _get_SELECT(self,group_id,cursor):
        '''
        
        :param group_id: 返回文章id在数据库中查询
        :param cursor: 
        :return: 
        '''
        sql_data = "SELECT * FROM jinridata \
                              WHERE 文章ID = '%s'" % group_id
        cursor.execute(sql_data)

        if self.cursor.rowcount != 0:

            return 'flase'

        return 'true'


if __name__=='__main__':
    dataout=dataout()







